package cn.hzxs.zjn.studentmanagement.controller;

import cn.hzxs.zjn.studentmanagement.domain.Page;
import cn.hzxs.zjn.studentmanagement.domain.Task;
import cn.hzxs.zjn.studentmanagement.domain.User;
import cn.hzxs.zjn.studentmanagement.sevice.TaskService;
import cn.hzxs.zjn.studentmanagement.sevice.UserService;
import cn.hzxs.zjn.studentmanagement.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: Zjn *Bring it on!!!*
 * @Date: 2021/3/28 14:02
 * Note:
 */
@RestController
public class TaskController {
    @Autowired
    TaskService taskService;
    @Autowired
    UserService userService;

    @GetMapping("/getTask")     /*获取任务*/
    public JsonResult getTask(@RequestParam(defaultValue = "0") Integer offset,
                              @RequestParam(defaultValue = "5") Integer limit) {
        HashMap<Object, Object> hashMap = new HashMap<>();
        HashMap<Object, Object> usersMap = new HashMap<>();
        List<Task> task = taskService.getTask(new Page(offset, limit));
        if (task.size() > 0) {
            for (int i = 0; i < task.size(); i++) {
                Map<String, String> usernameIconByAuthorId = userService.findUsernameIconByAuthorId(task.get(i).getAUTHORID());
                List<Map> usersinfo = taskService.getUserByTask(task.get(i).getTASKID());
                for (int j = 0; j < usersinfo.size(); j++) {
                    usersMap.put(j, usersinfo.get(j));
                }
                task.get(i).setUSERS(usersMap);
                task.get(i).setAUTHOR(usernameIconByAuthorId.get("user_name"));
                task.get(i).setAUTHORICON(usernameIconByAuthorId.get("user_icon"));
                hashMap.put(i, task.get(i));
            }
            return new JsonResult(hashMap);
        } else
            return new JsonResult(hashMap,"200","没有数据");
    }

    @PostMapping("/addTask")      /*添加任务*/
    public JsonResult addTask(@RequestBody Task task) {
        HashMap<Object, Object> hashMap = new HashMap<>();
        if (taskService.ifExistTitle(task)) {
            hashMap.put("status", "error");
            return new JsonResult(hashMap, "200", "任务已存在");
        } else {
            taskService.addTask(task);
        }
        task.setTASKID(taskService.selectTaskIdBy(task));
        for (int i = 0; i < task.getUSERSIDS().size(); i++) {
            taskService.insertPeopleByTask(task, task.getUSERSIDS().get(i));
        }
        hashMap.put("status", "ok");
        return new JsonResult(hashMap);
    }

    @PostMapping("/deleteTask")     /*删除任务*/
    public JsonResult deleteTask(@RequestBody Task task) {
        HashMap<Object, Object> hashMap = new HashMap<>();
        hashMap.put("status", "ok");
        taskService.deleteTask(task);
        return new JsonResult(hashMap);
    }

    @PostMapping("/updateTask")    /*更新任务进度*/
    public JsonResult updateTask(@RequestBody Task task) {
        HashMap<Object, Object> hashMap = new HashMap<>();
        hashMap.put("status", "ok");
        taskService.updateTask(task);
        return new JsonResult(hashMap);
    }
}
